<?php

define('JaxI','int');
define('JaxS','string');
define('JaxB','boolean');
define('JaxQ',-1);
define('JaxD',-2);
define('JaxN',-3);
define('JaxJ',-4);
define('JaxNo',-5);


class Jax
{
	public $flash=false;

	function flashError($description)
	{
		global $Jax;
		$title=$Jax->XVar('title','Error',array('menu'=>'centerPopUp'));
		$buttons=$Jax->XVar('buttons');
		$description=$Jax->XVar('description',$description);
		$Jax->respond($title.$buttons.$description);
	}

	function __construct()
	{
		global $_Jax;
		$_Jax = array();
		header('content-type:text/xml');
	}

	public function XVar($N,$V=false,$A=false)
	{
		$As=null;

		if(is_array($A))
		{
			foreach($A as $R=>$L)
			{
	
				$L=str_replace('&','&amp;',$L);
				$L=str_replace('"','&quot;',$L);
				
				$As.=" $R=\"$L\"";
			}
		}

//		$V=html_entity_decode($V, ENT_QUOTES, 'UTF-8');
		
		


		if(!$V||$V{0}=='<')
		return ($V !== false) ?"<$N$As>$V</$N>":"<$N$As/>";
		else
		return ($V !== false) ? "<$N$As><![CDATA[$V]]></$N>":"<$N$As/>";
	}

	public function error($E)
	{
		//		file_put_contents('debug.txt',$this->XVar('errors',$this->XVar('error',htmlentities($E)))."\r\n\r\n",FILE_APPEND);
		die($this->XVar('errors',$this->XVar('error',htmlentities($E))));
	}

	public function strict($V,$T)
	{
		if($T==JaxQ)
		$GLOBALS['_Jax'][$V] = addslashes($GLOBALS['_Jax'][$V]);
		else if($T==JaxN)
		$GLOBALS['_Jax'][$V] = !is_numeric($GLOBALS['_Jax'][$V]) ? null : (int) $GLOBALS['_Jax'][$V];
		else if($T==JaxJ)
		$GLOBALS['_Jax'][$V] = $GLOBALS['_Jax'][$V] ? 1 : 0;
		else if($T==JaxD)
		$GLOBALS['_Jax'][$V] = date('Y-m-d H:i:s',$GLOBALS['_Jax'][$V]); //2006-11-14 21:52:35
		else if($T==JaxNo)
		{}
		else
		settype($GLOBALS['_Jax'][$V],$T);
	}

	public function check($V,$D=false,$S=false)
	{
		if(isset($_REQUEST[$V]))
		$GLOBALS['_Jax'][$V]= $_REQUEST[$V];
		else
		$GLOBALS['_Jax'][$V] = $D;


		if($S)
		$this->strict($V,$S);
	}

	public function need($V,$M=false,$S=false)
	{
		if(!isset($_REQUEST[$V]))
		{
			if(!$M)
			$M = "Action Requires $V";

			if(!$this->flash)
			$this->error($M);
			else
			$this->flashError($M);
		}
		else
		$GLOBALS['_Jax'][$V]= $_REQUEST[$V];

		if($S)
		$this->strict($V,$S);
	}

	public function success($val=false)
	{
		return $this->XVar('success',$val);
	}

	public function respond($R,$html=false)
	{
		if(is_object($R))
		$R=$R->__toString();

		die($this->XVar('response',$R,array(),$html));
	}
}

function combineOBJ($array)
{
	$returnVal=false;
	foreach($array as $item)
	$returnVal.= is_object($item) ? $item->__toString() : $item;
	return $returnVal;
}

?>